<template>
  <div class="container">
    <img class="banner" src="@/assets/user-banner.png" alt="" />
    <div class="wrap">
      <div class="title">摄影师</div>
      <div class="list">
        <div class="col" v-for="item in list" :key="item.id">
          <router-link
            :to="{
              path: '/cameramanDetail',
              query: {
                id: item.id,
              },
            }"
          >
            <div class="item">
              <span class="pointer collect" @click.prevent="collect(item)">
                <i class="el-icon-star-on" v-if="item.follow == 1"></i>
                <i class="el-icon-star-off" v-else></i>
              </span>
              <img :src="item.avatar" alt="" />
              <div class="text">
                <p>{{ item.nickname }}</p>
                <span> {{ item.bio }}</span>
              </div>
            </div>
          </router-link>
        </div>
      </div>
      <pagination
        v-if="totalPage > 1"
        :total="total"
        :currentPage="page"
        :pageSize="rows"
        @handleCurrentChange="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: "NavBar",
  data() {
    return {
      list: [],
      page: 1,
      rows: 10,
      total: 0,
      totalPage: 0,
    };
  },
  components: {
    pagination: () => import("@/components/pagination.vue"),
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.$post("/index/cameraman", {
        page: this.page,
        rows: this.rows,
      }).then((res) => {
        this.list = res.data.data;
        this.total = res.data.total;
        this.totalPage = res.data.last_page;
      });
    },
    handleCurrentChange(e) {
      this.page = e;
      this.getList();
    },
    collect(item) {
      let that = this;
      this.$post("/user/follow", { follow_id: item.id }).then((res) => {
        if (res.code == 1) {
          if (res.data.result === "Y") {
            item.follow = 1;
            that.$message.success("关注成功");
          }
          if (res.data.result === "N") {
            item.follow = 0;
            that.$message.error("取消关注");
          }
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.banner {
  height: 300px;
}

.title {
  font-size: 58px;
  font-weight: bold;
  text-align: center;
  margin: 30px 0;
}
.list {
  display: flex;
  flex-wrap: wrap;
  margin: -14px;
  padding-bottom: 50px;
  .col {
    width: calc(100% / 4);
    padding: 14px;
    .item {
      position: relative;
      .collect {
        position: absolute;
        right: 0;
        top: 0%;
        padding: 6px 10px;
        color: #fff;
        background: rgba($color: #8f79c4, $alpha: 0.7);
        border-radius: 0 0 0 8px;
        i {
          display: block;
        }
      }
    }
    img {
      height: 280px;
      display: block;
    }
    .text {
      padding: 15px;
      background: #f7f7f7;
      span {
        color: #808080;
        font-size: 14px;
      }
    }
  }
}
@media screen and (max-width: 768px) {
  .title {
    font-size: 24px;
  }
  .list {
    margin: -8px;
    .col {
      width: 50%;
      padding: 8px;
      img {
        height: auto;
      }
    }
  }
}
</style>
